//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.qst.crop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.crop.dao.AddressDao;
import com.qst.crop.dao.UserDao;
import com.qst.crop.entity.User;
import com.qst.crop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Autowired
    private AddressDao addressDao;
    private Integer pageSize = 30;

    public UserServiceImpl() {
    }

    public List<User> selectAll() {
        return this.userDao.selectAll();
    }

    public void add(User user) {
        String password = user.getPassword();
        String encodePassword = this.passwordEncoder.encode(password);
        user.setPassword(encodePassword);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        this.userDao.insertSelective(user);
    }

    public void update(User user) {
        String password = user.getPassword();
        if (password != null) {
            String encode = this.passwordEncoder.encode(password);
            user.setPassword(encode);
        }

        user.setUpdateTime(new Date());
        this.userDao.updateByPrimaryKeySelective(user);
    }

    public void delete(String userName) {
        this.userDao.deleteByPrimaryKey(userName);
    }

    public User selectByUserName(String userName) {
        User user = this.userDao.selectByPrimaryKey(userName);
        return user;
    }

    public PageInfo<User> findPage(Integer pageNum) {
        PageHelper.startPage(pageNum, this.pageSize);
        List<User> users = this.userDao.selectAll();
        PageInfo<User> userPageInfo = new PageInfo(users);
        return userPageInfo;
    }

    public PageInfo<User> findPage(User user, Integer pageNum, Integer pageSize) {
        List<User> users = this.userDao.selectAll();
        PageInfo<User> pageInfo = new PageInfo(users);
        return pageInfo;
    }

    public void loginUpdateByUsername(User user) {
        user.setUpdateTime(new Date());
        this.userDao.updateByPrimaryKeySelective(user);
    }

    public Example createExample(User user) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(user.getUserName())) {
            criteria.andLike("userName", "%" + user.getUserName() + "%");
        }

        if (!StringUtils.isEmpty(user.getAddress())) {
            criteria.andLike("address", "%" + user.getAddress() + "%");
        }

        return example;
    }
}
